//
//  RootViewController.m
//  UI15_网络请求
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


/*get 和 post 请求区别
    get请求的url地址比较简单,易读,所有的参数比如name=@"张三"&pass=@"123456"都直接拼接在url路径后面;
 get格式的url地址可以直接放在浏览器里读取
 缺点:用户与in色信息会暴露在外面,不安全.
 post:特点是url地址中支包含部分参数,一部分参数单独存在body体重,body体最终以data二进制流形式存储,很安全
 相对get请求,post请求不易读,因为参数跟url路径是分开的.
 */

- (IBAction)getButton:(id)sender {
    //NSURLSession 网络请求类
    //NSURLConnection 已经被废弃
    //苹果封装了不同的网络请求任务类
   // NSURLSessionDataTask 用来创建get/post请求任务
    //NSURLSessionDownloadTask 实现下载任务的类
    //NSURLSessionUploadTask 实现上传任务的类
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    //ios9下的中文编码
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //创建系统识别的url对象
    NSURL *url = [NSURL URLWithString:urlEncode];
    //创建请求对象,默认是get请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //创建网络请求基本配置
    //比如多线程,异步请求;
    //比如后台请求等
    NSURLSessionConfiguration *configur = [NSURLSessionConfiguration defaultSessionConfiguration];
    //创建网络请求对象
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configur];
    //链接服务器.并生成请求任务对象
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"result = %@",result);
        
    }];
    
    //执行请求任务
    [task resume];
    
}
@end
